package Control;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.UserDao;
import Model.User;

/**
 * Servlet implementation class LoginController
 */
public class LoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name=request.getParameter("username");
		String pwd=request.getParameter("pwd");
		String path="Login.jsp";
		User u=new User();
		u.setU_name(name);
		UserDao ud=new UserDao();
		List<User> ls=ud.getByName(name);
		String responseMessage="";
		if(ls.size()==0){
			responseMessage="用户名不存在";
			path="Register.jsp";
		}
		else if(pwd.equals(ls.get(0).getU_pwd())){
			path="Userdata.jsp";
		}else{
			responseMessage="密码错误";
		}
		System.out.println("用户名="+name+"密码="+pwd);
		request.getRequestDispatcher(path).forward(request, response);
	}

		
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
